package tech.aistar.corejava.day04;

/**
 * Created by 李雨昂 on 2019/7/19.
 */
public class SortDemo {
    public static void main(String[] args) {
        int[] arr = {3,1,2,4,5,8,6};
        int[] temp;
        temp = selectionSort(arr);
        System.out.print("选择排序:");
        for(int f:temp){
            System.out.print(f);
        }
        System.out.println();
        System.out.print("冒泡排序:");
        temp = bubbleSort(arr);
        for(int f:temp){
            System.out.print(f);
        }
    }
    /**
     * 选择 冒泡 快排 希尔 归并 ....[出去面试前一周]
     * 关于数组的排序
     *
     * 选择排序思路
     * 3 1 2 4 5 8 6
     * 1 2 3 4 5 8 6
     *
     * @return
     */
    public static int[] selectionSort(int[] arr){
        if(arr == null || arr.length == 0){
            return new int[]{-1};
        }
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]){
                    arr[i] = arr[i] ^ arr[j];
                    arr[j] = arr[i] ^ arr[j];
                    arr[i] = arr[i] ^ arr[j];
                }
            }
        }
        return arr;
    }

    /**
     * 冒泡算法 - 俩俩进行比较
     * 3 1 2 4 5 8 6
     * 1 2 3 4 5 6 8
     * @param arr
     * @return
     */
    public static int[] bubbleSort(int[] arr){
        if(arr == null || arr.length == 0){
            return new int[]{-1};
        }
        if(arr.length == 1){
            return arr;
        }
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if(arr[j] > arr[j + 1]){
                    arr[j] = arr[j] + arr[j + 1];
                    arr[j + 1] = arr[j] + arr[j + 1];
                    arr[j] = arr[j] + arr[j + 1];
                }
            }
        }
        return arr;
    }

}
